package com.itheima.web.controller;

import cn.hutool.core.util.StrUtil;
import com.itheima.domain.vo.*;
import com.itheima.web.manager.UserManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
public class UserController {

    @Autowired
    private UserManager userManager;

    /**
     * 查询用户列表
     *
     * @param page     页码
     * @param pagesize 每页条数
     * @return pageBeanVo
     */
    @GetMapping("/manage/users")
    public PageBeanVo findUserList(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10") Integer pagesize) {
        return userManager.findUserList1(page, pagesize);
    }

    /**
     * 查询用户个人信息
     *
     * @param userId 用户id
     * @return 用户个人信息对象
     */
    @GetMapping("/manage/users/{userId}")
    public UserInfoVo findUserInfoBy(@PathVariable Long userId) {
        return userManager.finUserInfoById1(userId);
    }

    /**
     * 查询用户视频列表
     *
     * @param page     页码
     * @param pagesize 每页条数
     * @param uid      用户id
     * @return
     */
    @GetMapping("/manage/videos")
    public PageBeanVo findUserVideoList(@RequestParam(defaultValue = "1") Integer page,
                                        @RequestParam(defaultValue = "10") Integer pagesize,
                                        Long uid) {
        return userManager.finUserVideoList(page, pagesize, uid);

    }

    /**
     * 首页统计数据
     */
    @GetMapping("/dashboard/summary")
    public AnalysisSummaryVo summary() {
        return userManager.summary();
    }

    /**
     * 查询用户动态列表
     * 动态审核
     *
     * @param page     页码
     * @param pagesize 每页条数
     * @param uid      用户id
     * @param state    状态
     * @return
     */
    @GetMapping("/manage/messages")
    public PageBeanVo findMovementList(@RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer pagesize,
                                       Long uid,
                                       String state) {
        Integer stateInt = null;
        if (StrUtil.equals(state, "''")) {
            stateInt = null;
        } else if (StrUtil.isNotEmpty(state)) {
            //前端有坑 没有传递state的时候 传入了 ''空串  state 需要判断 这是前端的小bug
            stateInt = Integer.valueOf(state);//转换成int
        }

        PageBeanVo vo = userManager.findMovementListByUid(page, pagesize, uid, stateInt);
        //System.out.println(vo);
        return vo;
    }

    /**
     * 查询动态详情
     *
     * @param movementId 动态id
     * @return 动态封装对象
     */
    @GetMapping("/manage/messages/{movementId}")
    public MovementVo findMovementById(@PathVariable String movementId) {
        return userManager.findMovementById(movementId);
    }

    /**
     * 查询动态评论列表
     *
     * @param page      页码
     * @param pagesize  每页条数
     * @param messageID 动态id
     * @return pageBeanVo对象
     */
    @GetMapping("/manage/messages/comments")
    public PageBeanVo findCommentByMovementId(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize,
            String messageID
    ) {
        return userManager.findCommentByMovementId(page, pagesize, messageID);
    }
    //用户冻结操作
    @PostMapping("/manage/users/freeze")
    public Map<String, String> freeze(@RequestBody Map map) {
        Integer userId = Integer.valueOf (map.get ("userId").toString ());// 用户id
        Integer freezingTime = Integer.valueOf (map.get ("freezingTime").toString ());// 冻结时间
        Integer  freezingRange = Integer.valueOf (map.get ("freezingRange").toString ());// 冻结时间
        String reasonsForFreezing = map.get ("reasonsForFreezing").toString ();// 冻结原因
        String frozenRemarks = map.get ("frozenRemarks").toString ();// 冻结备注
        return userManager.freeze (userId, freezingTime, freezingRange, reasonsForFreezing, frozenRemarks);
    }
    //解冻
    @PostMapping("/manage/users/unfreeze")
    public Map unfreeze(@RequestBody Map map) {
        Integer userId = Integer.valueOf (map.get ("userId").toString ());
        return userManager.unfreeze (userId);
    }
    /**
     * 在首页中，根据时间显示图标数据: 新用户 活跃用户  次日留存。这些数据是需要基于用户的操作来进行统计
     */
    @GetMapping("/dashboard/users")
    public Map findDashboard(String sd,String ed,String type){
            return userManager.findDashboard(sd,ed,type);
    }
}
